﻿using App.Dal;
using App.Model;
using App.Untiys;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Win.Dal
{
    public class SysPailServices
    {
        static SqlSugarScope dbHelper = SqliteSugarHelper.Db;

        /// <summary>
        /// 获取样桶信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static SysPail GetInfo(SysPail model)
        {
            return dbHelper.Queryable<SysPail>().Where(m => m.Id == model.Id).First();
        }

        /// <summary>
        /// 获取样桶列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static List<SysPail> GetAllPageList()
        {
            return dbHelper.Queryable<SysPail>().ToList();
        }

        /// <summary>
        /// 获取样桶列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static List<SysPail> GetList()
        {
            return dbHelper.Queryable<SysPail>().Where(m => m.Disable == 0).ToList();
        }

        /// <summary>
        /// 获取样桶列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static SysPail GetOnePail(int id)
        {
            return dbHelper.Queryable<SysPail>().Where(m => m.Id == id).First();
        }

        /// <summary>
        /// 判断桶序号是否重复
        /// </summary>
        /// <param name="id"></param>
        /// <param name="PailId"></param>
        /// <returns></returns>
        public static bool PailidIsrepeat(int id, int PailId = 0)
        {
            if (id <= 0)
                return dbHelper.Queryable<SysPail>().Where(m => m.PailId == PailId).Any();
            else
                return dbHelper.Queryable<SysPail>().Where(m => m.PailId == PailId && m.Id != id).Any();
        }

        /// <summary>
        /// 判断桶名称是否重复
        /// </summary>
        /// <param name="id"></param>
        /// <param name="PailName"></param>
        /// <returns></returns>
        public static bool PailNameIsrepeat(int id, string PailName = "")
        {
            if (id <= 0)
                return dbHelper.Queryable<SysPail>().Where(m => m.PailName == PailName).Any();
            else
                return dbHelper.Queryable<SysPail>().Where(m => m.PailName == PailName && m.Id != id).Any();
        }


        /// <summary>
        /// 编辑样桶
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool Edit(SysPail model)
        {
            if (model.Id <= 0)
                return dbHelper.Insertable<SysPail>(model).InsertColumns(m => new { m.PailId, m.PailName, m.Current_Value, m.Disable, m.Threshold_Value, m.Current_Weight, m.Threshold_Weihght }).ExecuteCommand() > 0;
            return dbHelper.Updateable<SysPail>(model).UpdateColumns(m => new { m.PailId, m.PailName, m.Current_Value, m.Disable, m.Threshold_Value, m.Current_Weight, m.Threshold_Weihght, }).Where(m => m.Id == model.Id).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 修改当前点数
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool EditPoint(SysPail model)
        {
            if (model.Id <= 0)
                return dbHelper.Insertable<SysPail>(model).InsertColumns(m => new { m.PailId, m.PailName, m.Current_Value, m.Disable, m.Threshold_Value, m.Current_Weight, m.Threshold_Weihght }).ExecuteCommand() > 0;
            return dbHelper.Updateable<SysPail>(model).UpdateColumns(m => new { m.PailId, m.PailName, m.Current_Value, m.Disable, m.Threshold_Value, m.Current_Weight, m.Threshold_Weihght, }).Where(m => m.Id == model.Id).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool Delete(int id)
        {
            return dbHelper.Deleteable<SysPail>().Where(m => m.Id == id).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 重置当前桶数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool Resetting(int id)
        {
            return dbHelper.Updateable<SysPail>(new SysPail { Current_Value = 0, Current_Weight = 0, BatchCode = "" }).UpdateColumns(m => new { m.Current_Value, m.Current_Weight, m.BatchCode })
                .Where(m => m.Id == id).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 重置所有桶数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool ResettingAll()
        {
            return dbHelper.Updateable<SysPail>(new SysPail { Current_Value = 0, Current_Weight = 0, BatchCode = "" }).UpdateColumns(m => new { m.Current_Value, m.Current_Weight, m.BatchCode })
                .Where(m => 1 == 1).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 根据当前采样点数  获取可以放样的桶号
        /// </summary>
        /// <param name="pointCount">采样点数</param>
        /// <returns></returns>
        public static int GetPailIdForSampling(int pointCount)
        {
            int _iPail = 0;
            var _strSql = string.Empty;
            try
            {
                _strSql = $@"SELECT * FROM SysPail WHERE Current_Value + {pointCount} <= Threshold_Value AND Disable = 0
                             ORDER BY PailId ASC LIMIT 1";
                var data = dbHelper.SqlQueryable<SysPail>(_strSql).First();
                if (data != null)
                    _iPail = data.PailId;
            }
            catch (Exception ex)
            {
            }
            return _iPail;
        }

        /// <summary>
        ///  根据当前采样点数  获取可以放样的桶号
        /// </summary>
        /// <param name="pointCount"></param>
        /// <param name="batchCode"></param>
        /// <returns></returns>
        public static int GetPailIndexForSampling(int pointCount, string batchCode)
        {
            int _iPail = 0;
            var _strSql = string.Empty;
            try
            {
                var data = dbHelper.Queryable<SysPail>()
                      .Where(m => m.Disable == 0 && (m.Current_Value + pointCount) <= m.Threshold_Value)
                      .Where(m => m.BatchCode == batchCode)
                      .OrderBy(m => m.PailId, OrderByType.Asc)
                      .First();
                if (data != null)
                    _iPail = data.PailId;
                else
                {
                    data = dbHelper.Queryable<SysPail>()
                     .Where(m => m.Disable == 0 && (m.Current_Value + pointCount) <= m.Threshold_Value)
                     .Where(m => SqlFunc.IsNullOrEmpty(m.BatchCode))
                     .OrderBy(m => m.PailId, OrderByType.Asc)
                     .First();
                    if (data != null)
                        _iPail = data.PailId;
                }
            }
            catch (Exception ex)
            {

            }
            return _iPail;
        }

        /// <summary>
        /// 样桶更新点数
        /// </summary>
        /// <param name="pailName"></param>
        /// <returns></returns>
        public static bool SetPailValue(int pailId, int posCount, string supplierCode)
        {
            dbHelper.Updateable<SysPail>(new SysPail { BatchCode = supplierCode }).UpdateColumns(m => new { m.BatchCode })
            .Where(m => m.PailId == pailId).ExecuteCommand();
            return dbHelper.Updateable<SysPail>().SetColumns(it => it.Current_Value == it.Current_Value + posCount)
            .Where(it => it.PailId == pailId)
            .ExecuteCommand() > 0;
        }
    }
}
